#!/usr/bin/python


import os
import sqlite3
import sys

sys.stdout = open('/tmp/noddy.log', 'a')
sys.stderr = sys.stdout
print "started"


def register_transaction(action, ref, sha1, origin=""):
    if len(sha1) == 0:
        cursor.execute('INSERT INTO "transactions" (action, ref, origin) VALUES (?, ?, ?);', (action, ref, origin))
    else:
        cursor.execute('INSERT INTO "transactions" (action, ref, sha1, origin) VALUES (?, ?, ?, ?);', (action, ref, sha1, origin))


def get_rev_list(old, new):
    sha1s = []

    cmd = 'git rev-list --date-order --reverse ^%s %s' % (old, new)
    _in, out, err = os.popen3(cmd)

    for line in out:
        sha1s.append(line.strip())

    _in.close()
    out.close()
    err.close()

    return sha1s


def get_tag_origin(sha1):
    origin = ''

    cmd = 'git cat-file tag %s' % sha1
    _in, out, err = os.popen3(cmd)

    for line in out:
        if line.startswith('object'):
            origin = line[6:].strip()

    _in.close()
    out.close()
    err.close()

    return origin


def get_branch_origin(sha1):
    origin = ''

    cmd = 'git cat-file commit %s' % sha1
    _in, out, err = os.popen3(cmd)

    for line in out:
        if line.startswith('parent'):
            origin = line[6:].strip()

    _in.close()
    out.close()
    err.close()

    return origin


def new_ref(ref, sha1):
    print "new ref %s (%s)" % (ref, sha1[:8])
    if ref.startswith('refs/tags/'):
        print "  new tag %s" % ref
        register_transaction('tag', ref, sha1, get_tag_origin(sha1))
    elif ref.startswith('refs/heads/'):
        print "  new branch %s" % ref
        register_transaction('branch', ref, sha1, get_branch_origin(sha1))


def update_ref(ref, old, new):
    print "update ref %s %s->%s" % (ref, old[:8], new[:8])
    for commit in get_rev_list(old, new):
        print "register commit %s %s" % (ref, commit)
        register_transaction('commit', ref, commit)


def main():
    global cursor

    conn = sqlite3.connect('svnserver/db', isolation_level='IMMEDIATE')
    cursor = conn.cursor()

    for line in sys.stdin:
        old, new, ref = [x.strip() for x in line.split(' ', 2)]
        if old == '0' * 40:
            new_ref(ref, new)
        else:
            update_ref(ref, old, new)

    cursor.close()
    conn.commit()
    conn.close()


if __name__ == "__main__":
    main()

sys.stdout.close()
